import axios from "axios";
import store from "@/store";
import router from "@/router";
import { ElMessage, ElMessageBox } from "element-plus";
import type { Action } from "element-plus";
// console.log(process.env.VUE_APP_BASE_API);
const http = axios.create({
    baseURL: process.env.VUE_APP_BASE_API,
    // baseURL: "http://localtest.sxfarmer.com:8888",
    timeout: 10000,
    headers: {
        Accept: "application/json",
        "X-Requested-With": "XMLHttpRequest",
    },
});
/*
 * 请求拦截器，
 */
http.interceptors.request.use(
    (config) => {
        // 请求携带token
        const token = store.getters.token;
        if (token) {
            config.headers["Authorization"] = token;
        }
        // console.log(config.method);
        // if (config.method == "put") {
        //     config.headers["Content-Type"] = "image/jpeg";

        //     console.log(config);
        // }
        return config;
    },
    (error) => {
        console.log(error); // for debug
        return Promise.reject(error);
    }
);
/**
 * 返回拦截器，
 */
http.interceptors.response.use(
    (response) => {
        const res = response.data;
        // console.log(response, 222);
        // if (response.headers.server == "MinIO" && response.status == 200) {
        //     return true;
        // }
        if (!res.success) {
            // if (res.message) {
            //     ElMessage.error(res.message);
            // } else {
            //     ElMessage.error(res.msg);
            // }
            // console.log(res, 111);
            if (res) {
                errorHandle(res.code, res.msg);
            }

            return Promise.reject(res);
        }
        return res;
    },
    (error) => {
        //超时处理
        // console.log(error.response.data);
        if (String(error).includes("timeout")) {
            ElMessage.error("请求超时!");
        }
        if (String(error).includes("Network")) {
            ElMessage.error("网络错误!");
        }
        // errorHandle(error.response.data.status, error.response.data.message)
        return Promise.reject(error.response);
    }
);
export default http;
//错误处理
const errorHandle = (status: string, message: string) => {
    // 状态码判断
    /**
     * @param {Object} status
     * 状态码和后台约定，进行相应操作
     * 请求成功	200
     * 请求失败	400
     * 内部错误	500
     * 未授权	401
     * 不合法的参数	40035
     * 请求超时	408
     */
    switch (status) {
        case "A00004":
            // u20004: 未登录状态，跳转登录页
            // ElMessage.error(message);
            // ElMessage.error("token已过期");
            // ElMessageBox.alert("This is a message", "Title", {
            //     confirmButtonText: "OK",
            //     callback: (action: Action) => {
            //         console.log(action);
            //     },
            // });
            ElMessageBox.confirm("token已过期，请重新登录！", "系统提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            }).then(() => {
                sessionStorage.clear();
                // console.log(router);
                router.replace("/login");
                setTimeout(() => {
                    window.location.reload();
                }, 100);
            });

            break;
        case "A00001":
            ElMessage.error(message);
            break;
        case "500":
            ElMessage.error("内部服务器错误");
            break;
        case "u20003":
            // ElMessage.error(message)
            // router.back(-1)
            break;
        case "408":
            // ElMessage.error(message);
            break;
        default:
            console.log(message);
    }
};
